package com.heima.wemedia.controller.v1;


import com.heima.common.constants.CacheConstant;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.IWmSensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/sensitive")
public class WmSensitiveController {
    @Autowired
    private IWmSensitiveService wmSensitiveService;
    /**
     * 敏感词列表
     */

    @PostMapping("/list")
    public ResponseResult list(@RequestBody SensitiveDto dto){
        return wmSensitiveService.getByPage(dto);
    }

    @PostMapping("/save")
    @CacheEvict(cacheNames =  CacheConstant.SENSITIVE,allEntries = true)
    public ResponseResult save(@RequestBody WmSensitive wmSensitive){
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveService.save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @DeleteMapping("/del/{id}")
    @CacheEvict(cacheNames =  CacheConstant.SENSITIVE,allEntries = true)
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmSensitiveService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/update")
    @CacheEvict(cacheNames =  CacheConstant.SENSITIVE,allEntries = true)
    public ResponseResult updateById(@RequestBody  WmSensitive wmSensitive){
       if (ObjectUtils.isEmpty(wmSensitive)){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
       }
       wmSensitive.setCreatedTime(new Date());
       wmSensitiveService.updateById(wmSensitive);
       return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @GetMapping("/getAll")
    List<WmSensitive> listAll(){
        return wmSensitiveService.list();
    }
}

